package cn.livingCloud.security;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.context.Config;
import cn.livingCloud.context.SessionConstants;
import cn.livingCloud.entity.core.Employee;
import cn.livingCloud.entity.user.Member;
import cn.livingCloud.service.EmployeeService;
import cn.livingCloud.service.MemberService;
import cn.livingCloud.service.SettingService;
import cn.livingCloud.utils.SessionUtils;
import cn.livingCloud.wechat.config.WechatConfig;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by 唐旭 on 2015/12/31.
 */
public class MemberInterceptor implements HandlerInterceptor {
    private static final Logger LOG = Logger.getLogger(MemberInterceptor.class);
    private String auto_login_url = "";
    private transient MemberService service = BeanProvider.getBean(MemberService.class);
    private transient EmployeeService employeeService = BeanProvider.getBean(EmployeeService.class);
    private transient SettingService settingService = BeanProvider.getBean(SettingService.class);

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        Object currentMember = SessionUtils.getCurrentMember();
        if (currentMember != null) {
            return true;
        }
        if (Config.ENVIRONMENT.equals("development")) {
            LOG.debug("===================development模式=====================");
            LOG.debug("===================1.自动创建member=====================");
            Member member = service.createTestMember();
            SessionUtils.setAttribute(SessionConstants.MEMBER_KEY, member);
            LOG.debug("===================2.自动加载Employee=====================");
            Employee employee = employeeService.loadEmployeeByMember(member.getUuid());
            SessionUtils.setAttribute(SessionConstants.EMPLOYEE_KEY, employee);
            LOG.debug("===================自动创建member完成=====================");
            return true;
        }
        String url = buildRedirectUrl(httpServletRequest);
        httpServletResponse.sendRedirect(url);
        return false;
    }

    private String buildRedirectUrl(HttpServletRequest httpServletRequest) {
        String requestURI = httpServletRequest.getRequestURI() + "?" + httpServletRequest.getQueryString();
        String format = String.format(auto_login_url, requestURI);
        String url = Config.getDomainUrl() + format;
        return WechatConfig.getRedirectUserAuthorizeUrl(url, settingService.getAppid());
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }

    public String getAuto_login_url() {
        return auto_login_url;
    }

    public MemberInterceptor setAuto_login_url(String auto_login_url) {
        this.auto_login_url = auto_login_url;
        return this;
    }
}
